package board;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.*;

public class Dice extends JPanel
{
   // JLabels to display the die images from the rolls of the dice
      private JLabel die1JLabel;
      private JLabel die2JLabel;
      private JLabel totalJLabel;

   // JButtons to allow user to interact with game
      private JButton rollJButton;
 
   // file name and directory constants
      private final String FILE_PREFIX = "src/MonopolyGame/src/board/Images/die";
      private final String FILE_SUFFIX = ".jpg";

   // instance variables
      private int result = 0;   
	  private int die1;
	  private int die2;
         
   // no-argument constructor
    public Dice()
   {
      createUserInterface();
   }
   
   // create and position GUI components; register event handlers
   private void createUserInterface()
   {
      // get content pane for attaching GUI components
         //Dice contentPane = new Dice();

      // enable explicit positioning of GUI components
         this.setLayout( null );

      // set up die1JLabel
         die1JLabel = new JLabel();
         die1JLabel.setBounds( 40, 70, 64, 64 );
         this.add( die1JLabel );
      
      // set up die2JLabel
         die2JLabel = new JLabel();
         die2JLabel.setBounds( 100, 70, 64, 64 );
         this.add( die2JLabel );
      
	  //set up totalJLabel
		 totalJLabel = new JLabel();
		 totalJLabel.setBounds(70,120, 64, 70 );
		 this.add(totalJLabel);

      // set up rollJButton
         rollJButton = new JButton();
         rollJButton.setBounds( 50, 20, 88, 23 );
         rollJButton.setText( "Roll" );
         this.add( rollJButton );
         rollJButton.addActionListener(
            
         new ActionListener() // anonymous inner class
         {
            // event handler called when rollJButton is clicked
               public void actionPerformed ( ActionEvent event )
            {
               rollJButtonActionPerformed( event );
            }
         
         } // end anonymous inner class

      ); // end call to addActionListener
      
      // set properties of application's window
         /*setTitle( "RED DICE" ); // set title bar string
         setSize( 350, 250 );      // set window size
         setVisible( true );       // display window*/
      
   } // end method createUserInterface
   
   private void rollJButtonActionPerformed( ActionEvent event )
   {
      rollDice();  // roll dice
      
   } // end method rollJButtonActionPerformed
   
   // generate random die rolls
   private int rollDice()
   {
      // generate random die values
          die1 = (int)(Math.random() * 6 + 1);
          die2 = (int)(Math.random() * 6 + 1);
      
      // display the dice images
      displayDie( die1JLabel, die1 );
      displayDie( die2JLabel, die2 );
      
	  result = die1 + die2;
	  totalJLabel.setText("Total:" + result);
	 
	  return result; // return sum of dice values
	  
      
   } // end method rollDice
   
   // displays the die image
   private void displayDie( JLabel picDieJLabel, int face )
   {
      ImageIcon image = 
         new ImageIcon( FILE_PREFIX + face + FILE_SUFFIX );

      // display die images in picDieJLabel      
      picDieJLabel.setIcon( image );

   } // end method displayDie
   
   // main method
   /*public static void main( String args[] )
   {
      Dice application = new Dice();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

   } // end method main*/

} // end class 

